import { PrinterManager } from './../../utils/print/printer/printManager';
import { StoreSvr } from './../../services/store';
import { Component, Output, EventEmitter, OnInit } from '@angular/core';
@Component({
    selector: 'search-device',
    template: `
        <div class="flex-column h100">
            <div class="title bg-darkgray" (click)="changeName('interfaceType')">
                <i class="material-icons icon">navigate_before</i>
                <span>查找设备</span>
            </div>
            <div class="search-content h100">
                <div *ngIf="isFinding" class="search-desc">
                    正在查找设备，请稍候...
                </div>
                <div class="no-search" *ngIf="!isFinding">
                    <i class="material-icons icon-big color-warning margin-t-m">error</i>
                    <p class="margin-t-s">没有找到可用的设备，{{notFindReason}}</p>
                    <button class="btn-primary margin-t-s" (click)="findDevice()"><i class="material-icons">search</i>重新查找</button>
                </div>
                
            </div>
        </div>
    `,
    styleUrls: ['./printer.component.scss']
})

export class SearchDeviceComponent implements OnInit {
    @Output()
    changePageName: EventEmitter<any> = new EventEmitter<any>();
    constructor(
        private storeSvr: StoreSvr,
        private printerManager: PrinterManager
    ) { }
    private isFinding: boolean = true;
    private printType: string = "";
    private wifiHost: string = "";
    private notFindReason:string = "请检查连接是否正常或电源是否打开";

    ngOnInit() {
        this.printType = this.storeSvr.getPrintType();
        this.wifiHost = this.storeSvr.getHost();
        this.findDevice();
    }

    async findDevice() {
        this.isFinding = true;
        let url = this.storeSvr.adsAbout.url + "/printer/find";
        let hosts = [];
        let info = {};
        if (this.printType == "wifi") {
            hosts = this.wifiHost.split(":");
            info = {
                ip: hosts[0],
                port: hosts[1] ? Number(hosts[1]) : 9100,
                deviceType: "kds-printer"
            }
        }

        let findParams = {
            storeId: this.storeSvr.CurrentStore && this.storeSvr.CurrentStore.id,
            token: this.storeSvr.Token,
            type: this.printType,
            info: JSON.stringify(info)
        }

        try {
            let result = await this.printerManager.findDevice(url, findParams);
            if (result) {
                if (result.data && result.data.length) {
                    this.storeSvr.setDeviceData(result.data);
                    this.changeName('configDevice');
                }else if(result.data && !result.data.length){
                    this.notFindReason = "请检查连接是否正常或电源是否打开";
                    this.isFinding = false;
                }else {
                    this.notFindReason = result.msg;
                    this.isFinding = false;
                }
            }
        } catch (err) {
            console.log(err);
            this.isFinding = false;
        }

    }

    changeName(name) {
        this.changePageName.emit(name);
    }
}